*"----------------------------------------------------------------------
*"*"本地接口：
*"  TABLES
*"      IT_DATA STRUCTURE  ZSDS034
*"      ET_OUT STRUCTURE  ZSDS035
*"----------------------------------------------------------------------
DATA:is_data LIKE zsds034.
DATA:es_out LIKE zsds035.
DATA: ls_zsdt013 LIKE zsdt013,
      lt_zsdt013 LIKE TABLE OF  ls_zsdt013.

"BAPI.
DATA: ls_partn_cat     TYPE bapibus1006_head-partn_cat, "业务伙伴类别
      ls_central       TYPE bapibus1006_central,
      ls_central_organ TYPE bapibus1006_central_organ,           "SAP BP: 组织数据的 BAPI 结构
      ls_address       TYPE bapibus1006_address,
      lt_bapiadtel     LIKE TABLE OF bapiadtel,
      ls_bapiadtel     LIKE          bapiadtel,
      lt_bapicomrem    LIKE TABLE OF bapicomrem,
      ls_bapicomrem    LIKE          bapicomrem,
      lt_return        TYPE TABLE OF bapiret2,
      ls_return        TYPE          bapiret2,

      " 银行
      ls_bank          TYPE bapibus1006_bankdetail,
      lv_x.

" 详细数据

DATA: ls_header    TYPE cmds_ei_header,
      ls_customer  TYPE cmds_ei_extern,
      ls_vendor    TYPE vmds_ei_extern,
      lt_vendor    TYPE vmds_ei_extern_t,
      lt_customer  TYPE cmds_ei_extern_t,
      ls_company   TYPE cmds_ei_company,
      lt_company   TYPE cmds_ei_company_t,
      lt_company_v TYPE vmds_ei_company_t,
      ls_company_v TYPE vmds_ei_company,
      lt_sales     TYPE cmds_ei_sales_t,
      ls_sales     TYPE cmds_ei_sales,
      lt_function  TYPE cmds_ei_functions_t,
      ls_function  TYPE cmds_ei_functions,
      lt_tax       TYPE cmds_ei_tax_ind_t,
      ls_tax       TYPE cmds_ei_tax_ind,
      ls_mdata     TYPE cmds_ei_main,
      ls_vdata     TYPE vmds_ei_main.
" 客户
DATA: es_master_data_correct   TYPE cmds_ei_main,
      es_msg_correct           TYPE cvis_message,
      es_master_data_defective TYPE cmds_ei_main,
      es_msg_defective         TYPE cvis_message.
" 供应商
DATA: es_master_data_correct_v   TYPE vmds_ei_main,
      "  es_msg_correct           TYPE cvis_message,
      es_master_data_defective_v TYPE vmds_ei_main.
"  es_msg_defective         TYPE cvis_message.


DATA: lv_i TYPE sy-index .  " 序号
" 判断数据
IF it_data[] IS NOT INITIAL.

  SELECT partner,bpext
    INTO TABLE @DATA(lt_but000)
    FROM but000
    FOR ALL ENTRIES IN @it_data
    WHERE bpext = @it_data-bpext.

  LOOP AT it_data INTO is_data.
    lv_i = lv_i + 1.
    IF   is_data-name_org1 IS INITIAL " 组织名称
      OR is_data-bu_group IS INITIAL " 账户组
      OR is_data-bu_sort1 IS INITIAL " 搜索词
      OR is_data-bpext IS INITIAL   " 身份证/社会统一信用代码
      OR is_data-country IS INITIAL " 国家
*        OR is_data-region IS INITIAL  " 省 地区
      OR is_data-post_code1 IS INITIAL  " 邮编
      OR is_data-city1 IS INITIAL   " 地址
      OR is_data-remark IS INITIAL  " 联系人
      OR is_data-tel_number IS INITIAL   " 电话
      OR is_data-bankl IS INITIAL  " 银行代码
      OR is_data-bankn IS INITIAL  " 银行账户
*        OR is_data-counc IS INITIAL  " 县
*        OR is_data-cityc IS INITIAL  " 市
      OR is_data-vkorg IS INITIAL  " 销售组织
      OR is_data-vtweg IS INITIAL  " 分销渠道
      OR is_data-spart IS INITIAL  " 产品组
      OR is_data-taxkd IS INITIAL  "  税分类
      OR is_data-kalks IS INITIAL  " 定价过程
      OR is_data-waers IS INITIAL   " 货币码
      OR is_data-ktgrd IS INITIAL  " 客户科目分配组 Z1,Z2
      OR is_data-bukrs IS INITIAL  " 公司代码
      OR is_data-akont IS INITIAL   " 总账科目
      OR is_data-zterm IS INITIAL    " 付款条件  C000.
      OR is_data-pltyp IS INITIAL. " 价格清单

      es_out-zxh = lv_i.
      es_out-kunnr = ''.
      es_out-ztype = 'E'.
      es_out-zmsg = '必输值不能为空'.
      APPEND es_out TO et_out.
    ENDIF.

    IF es_out-ztype = ''.
      IF is_data-klabc <> 'C'. " klabc 为 C 表示为 服务站
        IF is_data-region IS INITIAL
        OR is_data-counc IS INITIAL  " 县
        OR is_data-cityc IS INITIAL.  " 市.
          es_out-zxh = lv_i.
          es_out-kunnr = ''.
          es_out-ztype = 'E'.
          es_out-zmsg = '创建客户时请输入省市县信息'.
          APPEND es_out TO et_out.
        ENDIF.
      ENDIF.
    ENDIF.


    IF es_out-ztype = ''.
      " 判断是否存在外部编号
      READ TABLE lt_but000 INTO DATA(ls_but000) WITH KEY bpext = is_data-bpext.
      IF sy-subrc <> 0.
        IF is_data-klabc <> 'C'.
          is_data-counc = is_data-counc+3(3). " 县代码为后三位.
          is_data-cityc = is_data-cityc+3(4). " 城市代码位后四位.
        ELSE.  " 服务站 省市县为空。
          is_data-counc = ''.
          is_data-cityc = ''.
          is_data-region = ''.
        ENDIF.

        " 补零
        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
          EXPORTING
            input  = is_data-akont
          IMPORTING
            output = is_data-akont.

        ls_partn_cat = '2'.       " 创建组织
        ls_central-searchterm1 = is_data-bu_sort1.  "搜索词*
        ls_central-partnerexternal = is_data-bpext.  "外部业务伙伴编号
        ls_central_organ-name1 = is_data-name_org1.  "组织名称*

        ls_address-postl_cod1 = is_data-post_code1.     "邮政编码*
        ls_address-city = is_data-city1.           "城市*
        ls_address-country = is_data-country.        "国家*
        ls_address-region = is_data-region.          "地区
        ls_address-langu = '1'.                   "语言*

        " 客户电话及名称
        ls_bapiadtel-country = is_data-country.
        ls_bapiadtel-telephone = is_data-tel_number.    "客户联系电话
        APPEND ls_bapiadtel TO lt_bapiadtel.
        CLEAR:ls_bapiadtel.

        CLEAR: ls_bapicomrem.
        ls_bapicomrem-comm_type = 'TEL'.
        ls_bapicomrem-comm_notes = is_data-remark.
        ls_bapicomrem-langu = '1'.
        ls_bapicomrem-consnumber =  001.
        APPEND ls_bapicomrem TO lt_bapicomrem.
        CLEAR: ls_bapicomrem.

        CALL FUNCTION 'BAPI_BUPA_CREATE_FROM_DATA'
          EXPORTING
*             BUSINESSPARTNEREXTERN   =
            partnercategory         = ls_partn_cat
            partnergroup            = is_data-bu_group
            centraldata             = ls_central
*             CENTRALDATAPERSON       =
            centraldataorganization = ls_central_organ
*             CENTRALDATAGROUP        =
            addressdata             = ls_address
*             DUPLICATE_MESSAGE_TYPE  =
*             ACCEPT_ERROR            = ' '
          IMPORTING
            businesspartner         = is_data-kunnr
          TABLES
            telefondata             = lt_bapiadtel
            communicationnotes      = lt_bapicomrem
            return                  = lt_return.

        CLEAR: lv_x,ls_partn_cat,ls_central,ls_central_organ,ls_address,lt_bapiadtel[],lt_bapicomrem[].
        LOOP AT lt_return INTO ls_return WHERE type CA 'AXE'.
          lv_x = 'X'.
          es_out-zmsg = es_out-zmsg && '|' && ls_return-message.
        ENDLOOP.

        IF lv_x IS NOT INITIAL.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
          es_out-kunnr = is_data-kunnr. " 客户编号
        ENDIF.

        IF is_data-kunnr IS  NOT INITIAL.
          " 生成锣响客户
          CLEAR: lt_return[],ls_return.
          CALL FUNCTION 'BAPI_BUPA_ROLE_ADD_2'
            EXPORTING
              businesspartner             = is_data-kunnr
              businesspartnerrolecategory = 'FLCU01'
              businesspartnerrole         = 'FLCU01'
            TABLES
              return                      = lt_return.
          CLEAR:lv_x.
          LOOP AT lt_return INTO ls_return WHERE type CA 'AXE'.
            lv_x = 'X'.
            es_out-zmsg = es_out-zmsg && '|' && ls_return-message.
          ENDLOOP.

          IF lv_x IS NOT INITIAL.
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          ELSE.

            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait = 'X'.
          ENDIF.
          CLEAR: lt_return[],ls_return.

          CALL FUNCTION 'BAPI_BUPA_ROLE_ADD_2'
            EXPORTING
              businesspartner             = is_data-kunnr
              businesspartnerrolecategory = 'FLCU00'
              businesspartnerrole         = 'FLCU00'
            TABLES
              return                      = lt_return.
          CLEAR:lv_x.
          LOOP AT lt_return INTO ls_return WHERE type CA 'AXE'.
            lv_x = 'X'.
            es_out-zmsg = es_out-zmsg && '|' && ls_return-message.
          ENDLOOP.

          IF lv_x IS NOT INITIAL.
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          ELSE.

            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait = 'X'.
          ENDIF.
          CLEAR: lt_return[],ls_return.

*-------------------------------------------------------------*
*修改时间: 04.01.2022 15:34:03  " 创建供应商服务站。
*-------------------------------------------------------------*
          IF is_data-klabc = 'C'.
            CALL FUNCTION 'BAPI_BUPA_ROLE_ADD_2'
              EXPORTING
                businesspartner             = is_data-kunnr
                businesspartnerrolecategory = 'FLVN00'
                businesspartnerrole         = 'FLVN00'
              TABLES
                return                      = lt_return.
            CLEAR:lv_x.
            LOOP AT lt_return INTO ls_return WHERE type CA 'AXE'.
              lv_x = 'X'.
              es_out-zmsg = es_out-zmsg && '|' && ls_return-message.
            ENDLOOP.

            IF lv_x IS NOT INITIAL.
              CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
            ELSE.

              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                EXPORTING
                  wait = 'X'.
            ENDIF.
            CLEAR: lt_return[],ls_return.

            CALL FUNCTION 'BAPI_BUPA_ROLE_ADD_2'
              EXPORTING
                businesspartner             = is_data-kunnr
                businesspartnerrolecategory = 'FLVN01'
                businesspartnerrole         = 'FLVN01'
              TABLES
                return                      = lt_return.
            CLEAR:lv_x.
            LOOP AT lt_return INTO ls_return WHERE type CA 'AXE'.
              lv_x = 'X'.
              es_out-zmsg = es_out-zmsg && '|' && ls_return-message.
            ENDLOOP.

            IF lv_x IS NOT INITIAL.
              CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
            ELSE.

              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                EXPORTING
                  wait = 'X'.
            ENDIF.
            CLEAR: lt_return[],ls_return.

          ENDIF.
*******	结束

          " 银行数据。
          ls_bank-bank_ctry = 'CN'. "国家代码
          ls_bank-bank_key = is_data-bankl. "银行代码
          ls_bank-bank_acct = is_data-bankn(18).
          ls_bank-bank_ref = is_data-bankn+18(2).
          CALL FUNCTION 'BAPI_BUPA_BANKDETAIL_ADD'
            EXPORTING
              businesspartner = is_data-kunnr
              bankdetailid    = '0001'
              bankdetaildata  = ls_bank
*       IMPORTING
*               BANKDETAILIDOUT =
            TABLES
              return          = lt_return.

          CLEAR:lv_x.
          LOOP AT lt_return INTO ls_return WHERE type CA 'AXE'.
            lv_x = 'X'.
            es_out-zmsg = es_out-zmsg && '|' && ls_return-message.
          ENDLOOP.

          IF lv_x IS NOT INITIAL.
            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
          ELSE.

            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait = 'X'.
          ENDIF.
          CLEAR: lt_return[],ls_return,ls_bank.

          "公司代码数据
          ls_header-object_instance-kunnr = is_data-kunnr.
          ls_header-object_task = 'M'.
          ls_customer-header = ls_header.

          ls_company-task = 'M'.
          ls_company-data_key-bukrs = is_data-bukrs.   "公司代码*
          ls_company-data-akont = is_data-akont.         "统驭科目*
          ls_company-datax-akont = 'X'.
          APPEND ls_company TO lt_company.
          CLEAR: ls_company.
          ls_customer-company_data-company = lt_company.  "公司数据


*******	供应商
          IF is_data-klabc = 'C'.
            ls_company_v-task = 'M'.
            ls_company_v-data_key-bukrs = is_data-bukrs.   "公司代码*
            ls_company_v-data-akont = '0022410600'.         "统驭科目* " 其他应付款-三包费
            ls_company_v-datax-akont = 'X'.
            APPEND ls_company_v TO lt_company_v.
            CLEAR: ls_company_v.
            ls_vendor-header-object_instance-lifnr = is_data-kunnr."i_data-lifnr.
            ls_vendor-header-object_task           = 'M'.
            ls_vendor-company_data-company = lt_company_v.
          ENDIF.




**通用数据-税收数据 " 县市代码
          ls_customer-central_data-central-data-counc = is_data-counc.
          ls_customer-central_data-central-data-cityc = is_data-cityc.
          ls_customer-central_data-central-datax-counc = 'X'.
          ls_customer-central_data-central-datax-cityc = 'X'.

          ls_customer-central_data-central-data-ktokd = is_data-bu_group.
          ls_customer-central_data-central-datax-ktokd = 'X'.

** 销售数据
          ls_sales-task = 'M'.
          ls_sales-data_key-vkorg =  is_data-vkorg.             "销售机构*
          ls_sales-data_key-vtweg =  is_data-vtweg.
          ls_sales-data_key-spart =  is_data-spart.
"2022.1.7添加客户组、客户统计组、价格组
          ls_sales-data-kdgrp =  is_data-kdgrp.
          ls_sales-data-versg =  is_data-versg.
          ls_sales-data-konda =  is_data-konda.
          ls_sales-datax-kdgrp =  'X'.
          ls_sales-datax-versg =  'X'.
          ls_sales-datax-konda =  'X'.
*****************************************************
          ls_sales-data-ktgrd =  is_data-ktgrd.      "客户科目分配组*
          ls_sales-data-zterm =  is_data-zterm.      "收付条件代码
          ls_sales-data-kalks =  is_data-kalks.      "客户定价过程*
          ls_sales-data-waers =  is_data-waers.      " 货币
          ls_sales-data-pltyp =  is_data-pltyp.


          IF is_data-klabc <> 'A' AND is_data-klabc <> 'C'.  " A表示只能特殊权限的人才能下单,C为服务站
            is_data-klabc = ''.
          ENDIF.
          ls_sales-data-klabc =  is_data-klabc.      " 客户权限
          ls_sales-datax-ktgrd =  'X'.
          ls_sales-datax-zterm =  'X'.
          ls_sales-datax-kalks =  'X'.
          ls_sales-datax-waers =  'X'.
          ls_sales-datax-klabc =  'X'.
          ls_sales-datax-pltyp =  'X'.


          ls_function-task = 'M'.
          ls_function-data_key-parvw = 'AG'.
          ls_function-data_key-parza = 1.
          ls_function-data-partner = is_data-kunnr.
          ls_function-datax-partner = 'X'.
          APPEND ls_function TO lt_function.

          ls_function-task = 'M'.
          ls_function-data_key-parvw = 'RE'.
          ls_function-data_key-parza = 2.
          ls_function-data-partner = is_data-kunnr.
          ls_function-datax-partner = 'X'.
          APPEND ls_function TO lt_function.

          ls_function-task = 'M'.
          ls_function-data_key-parvw = 'RG'.
          ls_function-data_key-parza = 3.
          ls_function-data-partner = is_data-kunnr.
          ls_function-datax-partner = 'X'.
          APPEND ls_function TO lt_function.

          ls_function-task = 'M'.
          ls_function-data_key-parvw = 'WE'.
          ls_function-data_key-parza = 4.
          ls_function-data-partner = is_data-kunnr.
          ls_function-datax-partner = 'X'.
          APPEND ls_function TO lt_function.

          ls_sales-functions-functions = lt_function.
          APPEND ls_sales TO lt_sales.
          ls_customer-sales_data-sales = lt_sales .
          CLEAR: ls_sales.

**税收标识
          ls_tax-task = 'M'.
          ls_tax-data_key-aland = 'CN'.               "国家
          ls_tax-data_key-tatyp = 'MWST'.            "税收类别 (销项税)
          ls_tax-data-taxkd = is_data-taxkd.                   "税分类*
          ls_tax-datax-taxkd = 'X'.
          APPEND ls_tax TO lt_tax.

          ls_customer-central_data-tax_ind-tax_ind = lt_tax.
**ls_mdata
          APPEND ls_customer TO lt_customer.

          ls_mdata-customers =  lt_customer.
*初始化
          CALL METHOD cmd_ei_api=>initialize.

*添加数据
          CALL METHOD cmd_ei_api=>maintain_bapi
            EXPORTING
              is_master_data           = ls_mdata
            IMPORTING
              es_master_data_correct   = es_master_data_correct
              es_message_correct       = es_msg_correct
              es_master_data_defective = es_master_data_defective
              es_message_defective     = es_msg_defective.

          CLEAR: ls_customer,lt_customer[].

          IF es_msg_defective-is_error = 'X'.
            ROLLBACK WORK.
            lt_return = es_msg_defective-messages.
            LOOP AT lt_return  INTO ls_return.
              es_out-zmsg = es_out-zmsg &&  '客户主数据创建成功，公司视图销售视图创建失败!' && '|' && ls_return-message.
            ENDLOOP.
          ELSE.
            COMMIT WORK.
          ENDIF.
          CLEAR: ls_mdata.

          IF is_data-klabc = 'C'.
            APPEND ls_vendor TO lt_vendor.
            ls_vdata-vendors = lt_vendor.
            CALL METHOD vmd_ei_api=>initialize.

            CALL METHOD vmd_ei_api=>maintain_bapi
              EXPORTING
*                 iv_test_run              = SPACE
*                 iv_collect_messages      = SPACE
                is_master_data           = ls_vdata
              IMPORTING
                es_master_data_correct   = es_master_data_correct_v
                es_message_correct       = es_msg_correct
                es_master_data_defective = es_master_data_defective_v
                es_message_defective     = es_msg_defective.
            IF es_msg_defective-is_error = 'X'.
              ROLLBACK WORK.
              lt_return = es_msg_defective-messages.
              LOOP AT lt_return  INTO ls_return.
                es_out-zmsg = es_out-zmsg &&  '客户主数据创建成功，供应商数据创建失败' && '|' && ls_return-message.
              ENDLOOP.
            ELSE.
              COMMIT WORK.
            ENDIF.

          ENDIF.

          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
            EXPORTING
              input  = is_data-kunnr
            IMPORTING
              output = is_data-kunnr.

          es_out-zxh = lv_i.

          es_out-kunnr  = is_data-kunnr.
          IF es_out-zmsg IS INITIAL .
            es_out-zmsg = is_data-kunnr && is_data-name_org1 &&  '创建成功！'.
            es_out-ztype = 'S'.
          ELSE.
            es_out-ztype = 'E'.
            es_out-zmsg = es_out-zmsg.
          ENDIF.

          MOVE-CORRESPONDING is_data TO ls_zsdt013.
          ls_zsdt013-ztime = sy-uzeit.
          ls_zsdt013-zdate = sy-datum.
          ls_zsdt013-ztype = es_out-ztype.
          ls_zsdt013-zmsg = es_out-zmsg.
          ls_zsdt013-kunnr = es_out-kunnr.
          APPEND ls_zsdt013 TO lt_zsdt013.
          CLEAR: ls_zsdt013.


          APPEND es_out TO et_out.
          CLEAR: es_out.

        ELSE.
          es_out-zxh = lv_i.
          es_out-kunnr  =  ''.
          es_out-ztype = 'E'.
          es_out-zmsg = es_out-zmsg.

          MOVE-CORRESPONDING is_data TO ls_zsdt013.
          ls_zsdt013-ztime = sy-uzeit.
          ls_zsdt013-zdate = sy-datum.
          ls_zsdt013-ztype = es_out-ztype.
          ls_zsdt013-zmsg = es_out-zmsg.
          ls_zsdt013-kunnr = es_out-kunnr.
          APPEND ls_zsdt013 TO lt_zsdt013.
          CLEAR: ls_zsdt013.

          APPEND es_out TO et_out.
          CLEAR: es_out.
        ENDIF.

      ELSE.

        es_out-zxh = lv_i.
        es_out-kunnr = ''.
        es_out-ztype = 'E'.
        es_out-zmsg = '税号/身份证号:' && is_data-bpext && '已存在客户' && ls_but000-partner.

        MOVE-CORRESPONDING is_data TO ls_zsdt013.
        ls_zsdt013-ztime = sy-uzeit.
        ls_zsdt013-zdate = sy-datum.
        ls_zsdt013-ztype = es_out-ztype.
        ls_zsdt013-zmsg = es_out-zmsg.
        ls_zsdt013-kunnr = es_out-kunnr.
        APPEND ls_zsdt013 TO lt_zsdt013.
        CLEAR: ls_zsdt013.

        APPEND es_out TO et_out.
        CLEAR: es_out,ls_but000..
      ENDIF.
    ENDIF.
    CLEAR: is_data,lt_return[],ls_return,es_out.
  ENDLOOP.

ELSE.

  es_out-zxh = lv_i.
  es_out-kunnr  =  ''.
  es_out-ztype = 'E'.
  es_out-zmsg = '数据为空'.

  MOVE-CORRESPONDING is_data TO ls_zsdt013.
  ls_zsdt013-ztime = sy-uzeit.
  ls_zsdt013-zdate = sy-datum.
  ls_zsdt013-ztype = es_out-ztype.
  ls_zsdt013-zmsg = es_out-zmsg.
  ls_zsdt013-kunnr = es_out-kunnr.
  APPEND ls_zsdt013 TO lt_zsdt013.
  CLEAR: ls_zsdt013.

  APPEND es_out TO et_out.
  CLEAR: es_out.
ENDIF.
MODIFY zsdt013 FROM TABLE lt_zsdt013.
CLEAR: lt_zsdt013[].